package com.lottery.im.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lottery.im.R;
import com.lottery.im.util.DisplayHelper;
import com.uuzuche.lib_zxing.DisplayUtil;

public class LotteryVideoView extends RelativeLayout {

  int mViewHeight = 0;

  private ConstraintLayout mLayoutStatus1;
  private ConstraintLayout mLayoutStatus2;
  private TextView mTvStatus;

  private ImageView mIvDs1;
  private ImageView mIvDs2;
  private ImageView mIvDs3;
  private ImageView mIvDs4;
  private ImageView mIvDs5;
  private ImageView mIvDx1;
  private ImageView mIvDx2;
  private ImageView mIvDx3;
  private ImageView mIvDx4;
  private ImageView mIvDx5;
  private TextView mTvOpen1;
  private TextView mTvOpen2;
  private TextView mTvOpen3;
  private TextView mTvOpen4;
  private TextView mTvOpen5;

  private TextView mTvTerm;
  private TextView mTvResult1;
  private TextView mTvResult2;
  private TextView mTvResult3;
  private TextView mTvResult4;
  private TextView mTvResult5;
  private TextView mTvResult;

  private boolean isOpening = false;
  private TranslateAnimation mShakeAnim;
  private Handler mHandler;

  public LotteryVideoView(Context context) {
    this(context, null);
  }

  public LotteryVideoView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public LotteryVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    // 开启硬件加速
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_lottery_video, this);
    view.findViewById(R.id.tv_close).setOnClickListener(v -> changeVisible());

    mLayoutStatus1 = view.findViewById(R.id.layout_status1);
    mLayoutStatus2 = view.findViewById(R.id.layout_status2);
    mTvStatus = view.findViewById(R.id.tv_status);

    mIvDs1 = view.findViewById(R.id.iv_ds1);
    mIvDs2 = view.findViewById(R.id.iv_ds2);
    mIvDs3 = view.findViewById(R.id.iv_ds3);
    mIvDs4 = view.findViewById(R.id.iv_ds4);
    mIvDs5 = view.findViewById(R.id.iv_ds5);
    mIvDx1 = view.findViewById(R.id.iv_dx1);
    mIvDx2 = view.findViewById(R.id.iv_dx2);
    mIvDx3 = view.findViewById(R.id.iv_dx3);
    mIvDx4 = view.findViewById(R.id.iv_dx4);
    mIvDx5 = view.findViewById(R.id.iv_dx5);
    mTvOpen1 = view.findViewById(R.id.tv_open1);
    mTvOpen2 = view.findViewById(R.id.tv_open2);
    mTvOpen3 = view.findViewById(R.id.tv_open3);
    mTvOpen4 = view.findViewById(R.id.tv_open4);
    mTvOpen5 = view.findViewById(R.id.tv_open5);

    mTvTerm = view.findViewById(R.id.tv_term);
    mTvResult1 = view.findViewById(R.id.tv_result1);
    mTvResult2 = view.findViewById(R.id.tv_result2);
    mTvResult3 = view.findViewById(R.id.tv_result3);
    mTvResult4 = view.findViewById(R.id.tv_result4);
    mTvResult5 = view.findViewById(R.id.tv_result5);
    mTvResult = view.findViewById(R.id.tv_result);

    mViewHeight = DisplayUtil.dip2px(getContext(), 20) + DisplayUtil.dip2px(getContext(), 30)
        + DisplayHelper.getScreenWidth(getContext()) * 597 / 1125;
  }

  /**
   * 开奖倒计时
   *
   * @param time 倒计时
   */
  public void setWaiting(String time) {

    if (isOpening) return;
    mLayoutStatus1.setVisibility(VISIBLE);
    mLayoutStatus2.setVisibility(GONE);
    mTvStatus.setText(time);
  }

  /**
   * 显示最后总的结果
   *
   * @param num 期号
   * @param str 开奖结果数组
   * @param result 总计以后拼接完成的开奖结果
   */
  public void setTermAndResult(String num, String[] str, String result) {
    mTvTerm.setText(num.substring(4, num.length()));
    mTvResult.setText(result);
    mTvResult1.setText(str[0]);
    mTvResult2.setText(str[1]);
    mTvResult3.setText(str[2]);
    mTvResult4.setText(str[3]);
    mTvResult5.setText(str[4]);
  }

  /**
   * 正在开奖（获取到开奖结果）
   *
   * @param str 开奖结果
   */
  public void setOpening(String[] str) {

    if (isOpening) return;

    isOpening = true;
    mLayoutStatus1.setVisibility(GONE);
    mLayoutStatus2.setVisibility(VISIBLE);

    mTvOpen1.setText(str[0]);
    mTvOpen2.setText(str[1]);
    mTvOpen3.setText(str[2]);
    mTvOpen4.setText(str[3]);
    mTvOpen5.setText(str[4]);

    if (Integer.valueOf(str[0]) % 2 == 0) {
      mIvDs1.setImageResource(R.drawable.bubble_shuang);
    } else {
      mIvDs1.setImageResource(R.drawable.bubble_dan_1);
    }
    if (Integer.valueOf(str[1]) % 2 == 0) {
      mIvDs2.setImageResource(R.drawable.bubble_shuang);
    } else {
      mIvDs2.setImageResource(R.drawable.bubble_dan_1);
    }
    if (Integer.valueOf(str[2]) % 2 == 0) {
      mIvDs3.setImageResource(R.drawable.bubble_shuang);
    } else {
      mIvDs3.setImageResource(R.drawable.bubble_dan_1);
    }
    if (Integer.valueOf(str[3]) % 2 == 0) {
      mIvDs4.setImageResource(R.drawable.bubble_shuang);
    } else {
      mIvDs4.setImageResource(R.drawable.bubble_dan_1);
    }
    if (Integer.valueOf(str[4]) % 2 == 0) {
      mIvDs5.setImageResource(R.drawable.bubble_shuang);
    } else {
      mIvDs5.setImageResource(R.drawable.bubble_dan_1);
    }

    if (Integer.valueOf(str[0]) > 4) {
      mIvDx1.setImageResource(R.drawable.bubble_da_1);
    } else {
      mIvDx1.setImageResource(R.drawable.bubble_xiao);
    }
    if (Integer.valueOf(str[1]) > 4) {
      mIvDx2.setImageResource(R.drawable.bubble_da_1);
    } else {
      mIvDx2.setImageResource(R.drawable.bubble_xiao);
    }
    if (Integer.valueOf(str[2]) > 4) {
      mIvDx3.setImageResource(R.drawable.bubble_da_1);
    } else {
      mIvDx3.setImageResource(R.drawable.bubble_xiao);
    }
    if (Integer.valueOf(str[3]) > 4) {
      mIvDx4.setImageResource(R.drawable.bubble_da_1);
    } else {
      mIvDx4.setImageResource(R.drawable.bubble_xiao);
    }
    if (Integer.valueOf(str[4]) > 4) {
      mIvDx5.setImageResource(R.drawable.bubble_da_1);
    } else {
      mIvDx5.setImageResource(R.drawable.bubble_xiao);
    }

    mShakeAnim = new TranslateAnimation(0, 0, 20, -20);
    mShakeAnim.setDuration(400);
    mShakeAnim.setRepeatCount(Animation.INFINITE);
    mShakeAnim.setRepeatMode(Animation.REVERSE);
    mIvDs1.setAnimation(mShakeAnim);
    mIvDs2.setAnimation(mShakeAnim);
    mIvDs3.setAnimation(mShakeAnim);
    mIvDs4.setAnimation(mShakeAnim);
    mIvDs5.setAnimation(mShakeAnim);
    mIvDx1.setAnimation(mShakeAnim);
    mIvDx2.setAnimation(mShakeAnim);
    mIvDx3.setAnimation(mShakeAnim);
    mIvDx4.setAnimation(mShakeAnim);
    mIvDx5.setAnimation(mShakeAnim);
    mShakeAnim.start();

    if (null == mHandler) {
      mHandler = new Handler(Looper.getMainLooper());
    }
    mHandler.postDelayed(mRunnable, 5000);
  }

  Runnable mRunnable = () -> {
    try {
      cancel();
    } catch (Exception e) {
    }
  };

  public void changeVisible() {
    if (getVisibility() == View.GONE) {
      animVisible();
    } else {
      animGone();
    }
  }

  private void animVisible() {
    ValueAnimator valueAnimator = createDropAnimator(1);
    valueAnimator.setDuration(300).start();
  }

  private void animGone() {
    ValueAnimator valueAnimator = createDropAnimator(2);
    valueAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        setVisibility(View.GONE);
      }
    });
    valueAnimator.setDuration(300).start();
  }

  private ValueAnimator createDropAnimator(final int type) {
    ValueAnimator valueAnimator = ValueAnimator.ofFloat(1, 1);
    valueAnimator.addUpdateListener(animation -> {
      ViewGroup.LayoutParams params = getLayoutParams();
      if (type == 1) {
        params.height = (int) (mViewHeight * animation.getAnimatedFraction());
        if (animation.getAnimatedFraction() > 0) setVisibility(View.VISIBLE);
      } else {
        params.height = (int) (mViewHeight * (1 - animation.getAnimatedFraction()));
      }
      setLayoutParams(params);
    });
    return valueAnimator;
  }

  public void cancel() {
    isOpening = false;
    mIvDs1.clearAnimation();
    mIvDs2.clearAnimation();
    mIvDs3.clearAnimation();
    mIvDs4.clearAnimation();
    mIvDs5.clearAnimation();
    mIvDx1.clearAnimation();
    mIvDx2.clearAnimation();
    mIvDx3.clearAnimation();
    mIvDx4.clearAnimation();
    mIvDx5.clearAnimation();
    if (null != mShakeAnim) {
      mShakeAnim.cancel();
      mShakeAnim = null;
    }
    if (null != mHandler) {
      mHandler.removeCallbacks(mRunnable);
      mHandler = null;
    }
  }
}
